/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Sony
 */
public class AbstractEntity {

    public Object add() {
        Object flag = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.save(this);
            tx.commit();
            flag = this;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean delete(Long id) {
        boolean flag = false;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            Object obj = session.get(this.getClass(), id);
            session.delete(obj);
            tx.commit();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean update(Long id) {
        boolean flag = false;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            Object obj = session.get(this.getClass(), id);
            Field updated_fields[] = this.getClass().getDeclaredFields();
            Field fields_to_be_updated[] = obj.getClass().getDeclaredFields();

            for (Field f1 : updated_fields) {
//System.out.println(“New Values :”+f1.getName());
                for (Field f2 : fields_to_be_updated) {
                    if (f1.getName().equals(f2.getName())) {
//System.out.println(“Set”);
                        f2.set(obj, f1.get(this));
                    }
                }
            }
            session.update(obj);
            tx.commit();
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public Object getById(Long id) {
        Object obj = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            obj = session.get(this.getClass(), id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
    
    public Object getByEmail(String email) {
        Object obj = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Query q=session.createQuery("from "+this.getClass().getName()+" where email='"+email+"'");
            obj=q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public List<? extends Object> getAll() {
        List<? extends Object> list = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from " + this.getClass().getName());
            list = query.list();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
      public Long getByName(String name) {
        Long id = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            id = (Long)session.get(this.getClass(), name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return id;
        
    }
      public static ArrayList<Object> page(List<Object> li)
      {
          ArrayList<Object> li_st=new ArrayList<Object>();
          
         //  ArrayList<Student> filtered_list = new ArrayList<>();
                int end = 10;
                if (li.size() < 10) {
                    end = li.size();
                }
                for (int i = 0; i < end; i++) {
                    System.out.println(li.get(0));
                  //  li_st.add(li.get(i));
                    li_st.add(li.get(i));
                }
          
          return li_st;
      }
      
        public List<? extends Object> getuserByStatus(boolean status) {
        List<? extends Object> list = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            Query query = session.createQuery("from " + this.getClass().getName()+" where status="+status+"" );
            list = query.list();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
    
      public boolean  UpdatePassword(String password,Long id)
    {
        boolean b=false;
        try 
        {
            Session s=HibernateUtil.getSessionFactory().openSession();
            Transaction tx=s.beginTransaction();
       
            Query q=s.createQuery("update "+this.getClass().getName()+" set password='"+password+"'  where id="+id+"");

            q.executeUpdate();
        
           tx.commit();
            b=true;
            
        } 
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return b;
    }
      
//       public List<? extends Object> GEtByStatus() {
//        List<? extends Object> list = null;
//        try {
//            Session session = HibernateUtil.getSessionFactory().openSession();
//            Query query = session.createQuery("from " + this.getClass().getName()+" where status='1'" );
//            list = query.list();
//            
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return list;
//    }
    
    
    
}
